<template>
  <div class="w-h-100 root-box">
    <div class="login-div">
      <div style="display: flex;flex-direction: row;">
        <el-avatar :src="iconSy" :size="60"></el-avatar>
        <span class="span-style">双英口语</span>
      </div>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item prop="telephone">
          <el-input placeholder="请输入手机号或学号!" clearable
                    v-model="ruleForm.telephone">
            <i slot="prefix" class="el-input__icon el-icon-user-solid"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" clearable show-password
                    placeholder="请输入密码"
                    v-model="ruleForm.password">
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="userType">
          <el-radio-group v-model="ruleForm.userType">
            <el-radio class="font-color" label="学生" value="1001"></el-radio>
            <el-radio class="font-color" label="教师" value="1002"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" class="login-button" type="primary" @click="login('ruleForm')">登录</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>
import iconStuent from "@/assets/img/studentLoginBp.png"
import iconTeacher from "@/assets/img/teacherLoginBp.jpg"
import {login} from "@/api/request";

export default {
  name: 'Login',
  data() {
    return {
      loading: false,
      iconSy: iconStuent,
      ruleForm: {
        telephone: '',
        password: '',
        userType: "学生",
      },
      rules: {
        telephone: [
          {required: true, message: '请输入手机号或学号!', trigger: 'blur'},
          {required: true, message: '长度在 3 到 20 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请选输入密码！'}
        ],
        userType: [
          {required: true, message: '请选择用户类型', trigger: 'change'}
        ],

      },
      aeee: '',
    }
  },
  methods: {

    login(formName) {
      this.loading = true
      const that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          return login(this.ruleForm).then(async res => {
            if (res.code === 200) {
              //将token存储到浏览器
              localStorage.setItem("Web-Token-stu", res.data.token)
              that.$store.dispatch("login", res.data)
              await this.$router.push("/videoUpload")
              this.$message.success("登录成功")
            } else {
              await this.$message.error(res.msg)
            }
            this.loading = false
          })
        } else {
          return false;
        }
      });
    },

  },
  watch: {
    "ruleForm.userType"(newValue) {
      if (newValue === '学生') {
        this.iconSy = iconStuent
      } else {
        this.iconSy = iconTeacher
      }
    }
  }

}
</script>

<style scoped>
.back-img {
  display: flex;
  background-color: rgba(0, 0, 0, 1);
  width: 100%;
  filter: brightness(50%);
}

.login-div {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.login-button {
  width: 320px;
}

.font-color {
  color: white;
}

.span-style {
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  font-weight: bold;
  font-size: 20px;
  margin: 25px 10px;
}

.root-box {
  display: flex;
  margin: 0;
  background-image: url('../../assets/img/loginBg.png');
  background-size: 100% 100%;
  background-attachment: fixed;
  justify-content: center;
  align-items: center;
}
</style>